<?php

namespace Model;

class System extends \Lib\Model {
	private $_aConfigInfo = array ();
	public function reloadConfig() {
		set_time_limit ( 0 );
		$this->checkUpload ();
		$aParam = array (
				'table' => 'config',
				'format' => array (
						'value' => array (
								'model' => 'System',
								'method' => 'formatValue',
								'param' => array (
										'$this->value' 
								),
								'rewrite' => true 
						) 
				) 
		);
		$aList = $this->DB->ls ( $aParam );
		foreach ( $aList ['items'] as $aRow ) {
			$aInfo [$aRow ['name']] = $aRow ['value'];
		}
		$this->Server->init ( true );
		$this->Config->set ( 'apk.java_path', $aInfo ['java_path'] );
		$this->Config->set ( 'info', $aInfo );
		$this->Config->save ( ROOT . '/config.inc.php' );
		return true;
	}
	public function formatValue($sValue) {
		$aDecodeValue = @json_decode ( $sValue, true );
		if ($aDecodeValue) {
			return $aDecodeValue;
		} else {
			return $sValue;
		}
	}
	public function checkUpload() {
		$aFile = $this->File->ls ( ROOT . DIRECTORY_SEPARATOR . $this->getFromConfig ( 'upload_path' ) );
		$sPackageName = $this->getFromConfig ( 'pkg_name' );
		$iPackageVersion = $this->getFromConfig ( 'pkg_version' );
		foreach ( $aFile as $sFile ) {
			if (basename ( $sFile ) == 'index.html') {
				continue;
			}
			$aApkInfo = $this->Apk->get ( $sFile );
			if ($aApkInfo ['package'] == $sPackageName && $aApkInfo ['version_code'] > $iPackageVersion) {
				$this->setToConfig ( 'pkg_version', $aApkInfo ['version_code'] );
				$this->setToConfig ( 'pkg_md5', $aApkInfo ['md5'] );
				$sUpdatePath = realpath ( ROOT . DIRECTORY_SEPARATOR . $this->getFromConfig ( 'update_path' ) );
				$sUpdateFile = $sUpdatePath . DIRECTORY_SEPARATOR . $aApkInfo ['package'] . '_' . $aApkInfo ['version_name'] . '.apk';
				rename ( $sFile, $sUpdateFile );
				$this->setToConfig ( 'update_file', basename ( $sUpdateFile ) );
			} else {
				@unlink ( $sFile );
			}
		}
	}
	public function getFromConfig($sName) {
		if (array_key_exists ( $sName, $this->_aConfigInfo )) {
			return $this->_aConfigInfo [$sName];
		}
		$aInfo = $this->DB->get ( 'config', array (
				'name' => $sName 
		) );
		$sValue = $this->formatValue ( $aInfo ['value'] );
		$this->_aConfigInfo [$sName] = $sValue;
		return $sValue;
	}
	public function setToConfig($sName, $sValue) {
		if (is_array ( $sValue )) {
			$sValue = json_encode ( $sValue );
		}
		$aInfo = $this->DB->get ( 'config', array (
				'name' => $sName 
		) );
		if ($aInfo) {
			$this->DB->update ( 'config', array (
					'value' => $sValue 
			), array (
					'name' => $sName 
			) );
		} else {
			$this->DB->insert ( 'config', array (
					'name' => $sName,
					'value' => $sValue 
			) );
		}
		$this->_aConfigInfo [$sName] = $sValue;
	}
	public function getPkgInfo() {
		$aInfo = $this->Config->get ( 'info' );
		
		$sUrl = $this->Config->get ( 'base.site_url' ) . '/' . $aInfo ['update_path'] . '/' . $aInfo ['update_file'];
		$sUrl = str_replace ( '/./', '/', $sUrl );
		$aPkgInfo = array (
				'package' => $aInfo ['pkg_name'],
				'version' => $aInfo ['pkg_version'],
				'md5' => $aInfo ['pkg_md5'],
				'url' => $sUrl 
		);
		return $aPkgInfo;
	}
}